/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lbim.server.main;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Jestan
 */
public class MapService {
    private static final Logger logger = LoggerFactory.getLogger(MapService.class);

    public static String[] COLORS = { "red", "blue", "brown", "purple", "yellow", "green", "gray", "orange", "black", "white" } ;
    public static String API_KEY1 = "ABQIAAAAIlO6w3POD_-zsgsAQsCxjxT2yXp_ZAY8_ufC3CFXhHIE1NvwkxTGamZqK2INfHFR_iTEVATUNlKRAA";
    public static String API_KEY ="ABQIAAAA8yt4k_6yzGrVHEfPj7sb1xT2yXp_ZAY8_ufC3CFXhHIE1NvwkxT6YA8ZZXulv4CHvVmWCLbQisgfYw";
    public static char markerSeparator = '|';

    private static HashMap<String,byte[]> mapCache = new HashMap<String,byte[]>(20);

    private static URI createUrl(String location, String res, String markers, String zoom) throws URISyntaxException {
        List<NameValuePair> qparams = new ArrayList<NameValuePair>();
        qparams.add(new BasicNameValuePair("center", location));
        qparams.add(new BasicNameValuePair("zoom", zoom));
        qparams.add(new BasicNameValuePair("size", res));
        qparams.add(new BasicNameValuePair("maptype", "mobile"));
        qparams.add(new BasicNameValuePair("format", "png8"));
        qparams.add(new BasicNameValuePair("markers", markers));
        qparams.add(new BasicNameValuePair("key", API_KEY));
        qparams.add(new BasicNameValuePair("sensor", "false"));
        URI uri = URIUtils.createURI("http", "maps.google.com", -1, "/staticmap", URLEncodedUtils.format(qparams, "UTF-8"), null);
        return uri;
    }

    public static byte[] getMap(String location, String res, String markers, String zoom) throws IOException, URISyntaxException {
        String key = location + res + markers + zoom;
        logger.info("URL "+location + ","+markers + zoom);
            byte[] data = getFromMapProvider(location, res, markers, zoom);
            return data;


    }

    private static byte[] getFromMapProvider(String location, String res, String markers, String zoom) throws IOException, URISyntaxException {
        URI uri = MapService.createUrl(location, res, markers, zoom);
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet get = new HttpGet(uri);
        HttpResponse response = httpClient.execute(get);
        logger.info(response.getStatusLine().toString());
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            return EntityUtils.toByteArray(entity);
        } else {
            return null;
        }
    }

}
